package com.efounder.form.comp;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import androidx.core.content.res.ResourcesCompat;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.LinearLayout;

import com.efounder.form.base.IComponent;
import com.efounder.form.util.FormCompUtil;
import com.efounder.frame.ViewSize;
import com.efounder.frame.arcmenu.item.CircleTextView;
import com.efounder.mobilecomps.R;
import com.efounder.util.AppContext;

/**
 * Created by hudq on 2016/10/10.
 */

public class FormParabolaAnimation extends View implements IComponent,IUIComponent {
    private Activity activity;
    private ViewGroup anim_mask_layout;//动画层
    private CircleTextView movingView;

    private int backgroundColor;

    private IUIComponent parentComp;


    public FormParabolaAnimation() {
        super(AppContext.getCurrentActivity());
        this.activity = AppContext.getCurrentActivity();
        //抛物线动画view
        movingView = new CircleTextView(activity);
        backgroundColor = ResourcesCompat.getColor(activity.getResources(), R.color.chat_red,null);

        anim_mask_layout = createAnimLayout();
        anim_mask_layout.addView(movingView);//把动画图片添加到动画层
    }
    /**
     * 抛物线 动画
     *
     */
    public void playParabolaAnimation(View fromView, View toView) {
//        final ImageView movingView = (ImageView) getLayoutInflater().inflate(R.layout.animator_image, null);
        //获取起点坐标
        int[] start_location = new int[2];
        fromView.getLocationInWindow(start_location);
        final int x1 = start_location[0];
        final int y1 = start_location[1];
        //设置动画图片的起始位置
        addViewToAnimLayout(movingView,start_location);

        //获取终点坐标，最近拍摄的坐标
        int[] location = new int[2];
        toView.getLocationInWindow(location);
        final int x2 = location[0];
        final int y2 = location[1];
        //此处控制偏移量
        int offsetX;
        int offsetY;
        offsetX = 0;//未做偏移，需要的自己计算
        offsetY = 0;
        //抛物线动画，原理：两个位移动画，一个横向匀速移动，一个纵向变速移动，两个动画同时执行，就有了抛物线的效果。
        ObjectAnimator translateAnimationX = ObjectAnimator.ofFloat(movingView, "translationX", 0, -(x1 - x2) - offsetX);
        translateAnimationX.setDuration(300);
        translateAnimationX.setInterpolator(new LinearInterpolator());
        ObjectAnimator translateAnimationY = ObjectAnimator.ofFloat(movingView, "translationY", 0, y2 - y1 + offsetY);
        translateAnimationY.setDuration(300);
        translateAnimationY.setInterpolator(new AccelerateInterpolator());
        //到终点后的缩小动画
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(movingView, "scaleX", 1, 0);
        scaleX.setDuration(200);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(movingView, "scaleY", 1, 0);
        scaleY.setDuration(200);
        scaleY.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                anim_mask_layout.removeView(movingView); //动画结束后移除动画图片
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
        AnimatorSet animatorSet = new AnimatorSet();//设置动画播放顺序
        animatorSet.play(translateAnimationX).with(translateAnimationY);
        animatorSet.play(scaleX).with(scaleY).after(translateAnimationX);
        animatorSet.start();
    }

    /**
     *  创建动画层
     *
     */
    private ViewGroup createAnimLayout() {
        ViewGroup rootView = (ViewGroup) activity.getWindow().getDecorView();
        LinearLayout animLayout = new LinearLayout(activity);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        animLayout.setLayoutParams(lp);
        animLayout.setId(Integer.MAX_VALUE << 1);
        animLayout.setBackgroundResource(android.R.color.transparent);
        rootView.addView(animLayout);
        return animLayout;
    }

    private void addViewToAnimLayout(final View view, int[] location) {
        int x = location[0];
        int y = location[1];
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.leftMargin = x;
        lp.topMargin = y;
        view.setLayoutParams(lp);
    }

    public int getBackgroundColor() {
        return backgroundColor;
    }

    public void setBackgroundColor(int backgroundColor) {
        this.backgroundColor = backgroundColor;
        movingView.setBackgroundColor(backgroundColor);
    }

    @Override
    public String getID() {
        return null;
    }

    @Override
    public void setID(String s) {

    }

    @Override
    public void setLayout() {
        FormCompUtil.setLayoutSize(this);
    }

    @Override
    public void creationComplete() {
        movingView.setBackgroundColor(backgroundColor);
    }

    @Override
    public float getX() {
        return 0;
    }

    @Override
    public void setX(float v) {

    }

    @Override
    public float getY() {
        return 0;
    }

    @Override
    public void setY(float v) {

    }

    @Override
    public int getGap() {
        return 0;
    }

    @Override
    public void setGap(int i) {

    }

    @Override
    public int getCompWidth() {
        return 0;
    }

    @Override
    public void setCompWidth(int i) {

    }

    @Override
    public int getCompHeight() {
        return 0;
    }

    @Override
    public void setCompHeight(int i) {

    }

    @Override
    public int getPercentWidth() {
        return 0;
    }

    @Override
    public void setPercentWidth(int i) {

    }

    @Override
    public int getPercentHeight() {
        return 0;
    }

    @Override
    public void setPercentHeight(int i) {

    }

    @Override
    public ViewSize getViewSize() {
        return null;
    }

    @Override
    public void setViewSize(ViewSize viewSize) {

    }

    @Override
    public IUIComponent getParentComp() {
        return parentComp;
    }

    @Override
    public void setParentComp(IUIComponent comp) {
        parentComp = comp;
    }
}
